/**
 * create ec ad
 */
define(['../../common/service', './step1','./step2','./step3','./step4','./createAdUtils'],
    function (http,step1,step2,step3,step4,dataUtils) {
        var CreateAd = {
            _publishParamCache :{}
            ,init: function () {
                var _this = this;
                _this._publishParamCache = dataUtils.publishParamCache;
                /**********  step0 app 账号查询 ***********/
                _this.init_apps();
            }
            ,init_apps:function(){
                var _this = this;
                http.do('/ec/allWebSiteByUserId ',null,'GET').then(function(res) {
                    if (res.flag == 'fail') {
                        Kit.alert2(res.errorMsg);
                        return;
                    }
                    $.each(res.data,function(i,obj){
                        //处理渲染的数据
                        obj.id = obj.productManageProductId + "_" + obj.pageId;
                        obj.text = obj.companyName;
                    });
                    $("div.ndp-drop-wrapper[name=product-drop]").empty().drop({
                        caret:"glyphicon-menu-right",
                        data:res.data,
                        name:"product"//为了方便serialize 建议设置name 属性
                    }).off("ITEM_CHANGE").on("ITEM_CHANGE",function(e){//下拉选择项点击事件
                        //初始化feed,account
                        var productId = (e.originalEvent.data.val + "").split("_")[0];
                        var pageId = (e.originalEvent.data.val + "").split("_")[1];
                        _this.init_feeds(productId);
                        _this._publishParamCache.productId = productId;
                        dataUtils.pageId = pageId;
                    }).val({id : -1, text : I18N.getText('createAd','chooseProduct')});
                    _this.init_feeds();
                });
            }
            ,init_feeds:function(prodId){
                var _this = this;
                if (prodId){
                    http.do('/feed/findByProd',{prodId:prodId},'GET').then(function(res) {
                        if (res.flag == 'fail') {
                            Kit.alert2(res.errorMsg);
                            return;
                        }
                        $.each(res.data, function (i, obj) {
                            //处理渲染的数据
                            obj.text = obj.feedName;
                        });
                        $("div.ndp-drop-wrapper[name=feed-drop]").empty().drop({
                            caret:"glyphicon-menu-right",
                            data:res.data,
                            name:"feed"//为了方便serialize 建议设置name 属性
                        }).off("ITEM_CHANGE").on("ITEM_CHANGE",function(e){//下拉选择项点击事件
                            //初始化 productSet
                            _this.init_productSet(e.originalEvent.data.val);
                            _this._publishParamCache.weFeedId=e.originalEvent.data.val;
                        }).val({id : -1, text : I18N.getText('createAd','chooseFeed')});
                    });
                } else {
                    $("div.ndp-drop-wrapper[name=feed-drop]").empty().drop({
                        caret:"glyphicon-menu-right",
                        data:[],
                        placeholder : "请选择feed",
                        name:"feed"//为了方便serialize 建议设置name 属性
                    }).val({id : -1, text : I18N.getText('createAd','chooseFeed')});
                }

                _this.init_productSet();
            }
            ,init_productSet:function(feedId){
                var _this = this;
                if (feedId){
                    http.work('/productSet/findByFeed',{"feed_ids":[Number(feedId)]},'POST').then(function(res) {
                        if (res.flag == 'fail') {
                            Kit.alert2(res.errorMsg);
                            return;
                        }
                        var productSetData = [];
                        $.each(res.data, function (i, obj) {
                            //处理渲染的数据
                            obj.text = obj.name;
                            productSetData.push(obj);
                        });
                        dataUtils.productSets = productSetData;
                        $("div.ndp-drop-wrapper[name=productSet-drop]").empty().drop({
                            caret:"glyphicon-menu-right",
                            data:productSetData,
                            name:"productSet"//为了方便serialize 建议设置name 属性
                        }).off("ITEM_CHANGE").on("ITEM_CHANGE",function(e){//下拉选择项点击事件
                            _this.init_accounts(_this._publishParamCache.productId);
                            console.log(e.originalEvent.data)
                            dataUtils.publishParamCache.productSetId=e.originalEvent.data.val;
                            dataUtils.selectedProductSet = {val : e.originalEvent.data.val, text :  e.originalEvent.data.text}
                            //修改产品系列 需要重新生成受众
                            //step1.init();
                        }).val({id : -1, text : I18N.getText('createAd','chooseProductSet')});
                    });
                } else {
                    $("div.ndp-drop-wrapper[name=productSet-drop]").empty().drop({
                        caret:"glyphicon-menu-right",
                        data:[],
                        placeholder :I18N.getText('createAd','chooseProductSet'),
                        name:"productSet"//为了方便serialize 建议设置name 属性
                    }).val({id : -1, text : I18N.getText('createAd','chooseProductSet')});
                }

                _this.init_accounts();
            }
            ,init_accounts:function(prodId){
                var _this = this;
                $("div.ndp-drop-wrapper[name=account-drop]").empty().drop({
                    caret:"glyphicon-menu-right",
                    data: [],
                    placeholder : I18N.getText('createAd','chooseAccount'),
                    name:"account"//为了方便serialize 建议设置name 属性
                }).val({id : -1, text : I18N.getText('createAd','chooseAccount')});
                _this.isFold();

                if (prodId){
                    http.do('/ec/adActs',{prodId:prodId},'GET').then(function(res) {
                        //后台有错。
                        if (res.flag == 'fail') {
                            Kit.alert2(res.errorMsg);
                            return;
                        }
                        var accountData = [];
                        $.each(res.data, function (i, obj) {
                            //处理渲染的数据
                            var tempObj = {};
                            tempObj.id = obj.fbAccountId;
                            tempObj.text = obj.name;
                            accountData.push(tempObj);
                        });
                        $("div.ndp-drop-wrapper[name=account-drop]").empty().drop({
                            caret:"glyphicon-menu-right",
                            data: accountData,
                            name:"account"//为了方便serialize 建议设置name 属性
                        }).off("ITEM_CHANGE").on("ITEM_CHANGE",function(e){//下拉选择项点击事件
                            dataUtils.publishParamCache.fbAccountId=e.originalEvent.data.val;
                            _this.isFold();
                        }).val({id : -1, text : I18N.getText('createAd','chooseAccount')});
                    });
                }
            }
            , isFold : function(){
                var _this = this;
                var _productDropV = $("div.ndp-drop-wrapper[name=product-drop]").data("val");
                var _feedDropV =  $("div.ndp-drop-wrapper[name=feed-drop]").data("val");
                var _productSetDropV = $("div.ndp-drop-wrapper[name=productSet-drop]").data("val");
                var _accountDropV = $("div.ndp-drop-wrapper[name=account-drop]").data("val");
                if (_productDropV == -1 || _feedDropV == -1 || _productSetDropV == -1 || _accountDropV == -1){
                     //只要有一个没选
                    _this.unFoldPage();
                } else {
                    //目标全选 展开
                    _this.foldPage();
                }
            }
            , unFoldPage: function () {
                console.log("展开动画");
                $("#app .pic-wrapper").animate({height: '310px'}, "slow");
                $("#app .ul-wrapper").animate({'margin-top': '85px'}, "slow");
                $("div[name='stepping']").hide();
            }
            , foldPage: function () {
                var _this = this;
                console.log("合上动画");
                $("#app .pic-wrapper").animate({height: '136px'}, "slow");
                $("#app .ul-wrapper").animate({'margin-top': '15px'}, "slow");
                $("div[name='stepping']").show();
                //初始化剩余的内容
                step1.init();
                step2.init();
                step3.init();
                step4.init();
                _this.initPublishParam();
            }
            ,initPublishParam:function(){
                //预算出价
                $("#budget .activeType-wrapper").css("visibility","hidden");
                $("input.budget-input").val('');
                $("input.price-input").val('');
                //
            }
        }
        return CreateAd;
    });
